<template>
  <div>
    <div>
      <!-- 地址获取 -->

    </div>
    <span align="center">外卖购物车</span>
    <div>
    <br>
      <span v-for="(i, index) in cartlist" :key="index">
       <b>{{ i.name }}</b><br>
       <span style="color:grey">{{i.description}}</span><br>
        ￥{{ i.price}}<br><br>

      </span>
      <van-radio-group v-model="radio" direction="horizontal">
        <van-radio name="1">微信</van-radio>
        <van-radio name="2">支付宝</van-radio>
      </van-radio-group>

      <van-submit-bar button-text="提交订单" @submit="onSubmit" />
    </div>
  </div>
</template>

<script>
import axios from '@/comm/api'
export default {
  data() {
    return {
      cartlist: [],
      radio:'',
      message:'',
    };
  },
  mounted() {
      this.getcartlist()
  },

  methods: {
    getcartlist() {
      axios.get("/mycart?userid=20").then((resp) => {
        console.log("购物者", resp);
        this.cartlist = resp.data.clist;
        this.tmoney = resp.data.tmoney;
        this.tcount = resp.data.tcount;
      });
    },
    onSubmit(){
      axios.post('/add_orders',{
        userid:20,
        address_id:this.cartlist[0]['id'],
        message:this.message,
        paytype:this.radio
      }).then((resp) => {
        if (resp.data.code==200){
          var order_code=resp.data.order_code
          //获取支付URL
          this.geturl(order_code)
        }
        console.log('创建订单成功',resp)
      })

    },
    geturl(order_code){
        axios.get('/getpayurl?order_code='+order_code).then((resp) => {
          var url=resp.data.url
          console.log('123',url)
          window.location.href=url
        })
    }
  },
};
</script>

<style scoped>



</style>
